package com.sgb.packers.widget;

import android.content.Context;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.sgb.packers.R;
import com.sgb.younghow.calendarview.Calendar;
import com.sgb.younghow.calendarview.CalendarView;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.util.DateUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 * @author : younghow
 * @date : 2021/11/29 15:18
 * description:
 */
public class DayWorkPopWindow extends BasePopWindow implements CalendarView.OnCalendarMultiSelectListener, CalendarView.OnCalendarInterceptListener, CalendarView.OnCalendarSelectListener, CalendarView.OnMonthChangeListener {

    private CalendarView mCalendarView;
    private TextView mTvTitle;
    private List<Calendar> mCalendar;
    private Calendar calendar;
    private TransCalender mTransCalender;
    private TextView mTvDate;

    public void setTransCalender(TransCalender mTransCalender) {
        this.mTransCalender = mTransCalender;
    }

    public DayWorkPopWindow(Context context) {
        super(context, DIR_DOWN_UP);
    }

    @Override
    protected int popLayout() {
        return R.layout.packers_pop_window_day_work;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        mCalendarView = holder.getPop().findViewById(R.id.calendarView);
        mTvTitle = holder.getPop().findViewById(R.id.tv_title);
        TextView tvPopSure = holder.getPop().findViewById(R.id.tv_pop_sure);
        TextView tvPopCancel = holder.getPop().findViewById(R.id.tv_pop_cancel);
        mTvDate = holder.getPop().findViewById(R.id.tv_date);
        ImageView mIvPre = holder.getPop().findViewById(R.id.iv_pre);
        ImageView mIvNext = holder.getPop().findViewById(R.id.iv_next);

        mCalendar = new ArrayList<>();
        mCalendarView.setOnCalendarSelectListener(this);
        //设置日期拦截事件，当前有效
        mCalendarView.setOnCalendarInterceptListener(this);
        mCalendarView.setOnMonthChangeListener(this);

        this.setHeight(WindowManager.LayoutParams.MATCH_PARENT);
        this.setWidth(WindowManager.LayoutParams.MATCH_PARENT);
        this.setClippingEnabled(false);

        mIvPre.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCalendarView.scrollToPre(false);
            }
        });

        mIvNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCalendarView.scrollToNext(false);
            }
        });

        tvPopSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (calendar != null){
                    mCalendar.clear();
                    mCalendar.add(calendar);
                }
                mTransCalender.transCalender(mCalendar);
            }
        });

        tvPopCancel.setOnClickListener(v -> dismiss());

        String[] split = DateUtils.timestamp2DateNumber(System.currentTimeMillis());
        mCalendarView.setWeeColor(0,0xff666666);
        mCalendarView.setRange(2000, 1, 1, Integer.parseInt(split[0]), Integer.parseInt(split[1]), Integer.parseInt(split[2]));
    }

    @Override
    public void onCalendarMultiSelectOutOfRange(Calendar calendar) {

    }

    @Override
    public void onMultiSelectOutOfSize(Calendar calendar, int maxSize) {

    }

    @Override
    public void onCalendarMultiSelect(Calendar calendar, int curSize, int maxSize) {

    }

    @Override
    public boolean onCalendarIntercept(Calendar calendar) {
        return calendar.getYear() > mCalendarView.getCurYear() ||
                (calendar.getYear() == mCalendarView.getCurYear() && calendar.getMonth() > mCalendarView.getCurMonth()) ||
                (calendar.getYear() == mCalendarView.getCurYear() && calendar.getMonth() == mCalendarView.getCurMonth() &&
                        calendar.getDay() > mCalendarView.getCurDay());
    }

    @Override
    public void onCalendarInterceptClick(Calendar calendar, boolean isClick) {

    }

    @Override
    public void onCalendarOutOfRange(Calendar calendar) {

    }

    @Override
    public void onCalendarSelect(Calendar calendar, boolean isClick) {
        this.calendar = calendar;
        mTvTitle.setText("选择日期");
    }

    @Override
    public void onMonthChange(int year, int month) {
        mTvDate.setText(String.format(Locale.CHINA, "%d年%d月", year, month));
    }

    public interface TransCalender {
        void transCalender(List<Calendar> list);
    }

    public void setSelect(Calendar... calendars) {
        mCalendarView.putMultiSelect(calendars);
    }

    public void setDefaultCalendar(Calendar... calendars){
        for (Calendar calendar : calendars) {
            if (!mCalendar.contains(calendar)) {
                mCalendar.add(calendar);
            }
        }
    }

    public void setSelectYearMonth() {
        mTvDate.setText(String.format(Locale.CHINA, "%d年%d月", mCalendar.get(mCalendar.size() - 1).getYear(), mCalendar.get(mCalendar.size() - 1).getMonth()));
        mCalendarView.scrollToCalendar(mCalendar.get(mCalendar.size() - 1).getYear(), mCalendar.get(mCalendar.size() - 1).getMonth(), mCalendar.get(mCalendar.size() -1).getDay());
    }

    public void setMaxSelectNumber(int number) {
        mCalendarView.setMaxMultiSelectSize(number);
    }
}
